// pages/interactionadd/interactionadd.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    list:[],
    userInfo:'',
    focus:false,
    replytex: '写评论',
    inputname:'',
    activityId:'',
    parentId:'',
    isFather:true,
  },
  getdetails(){
    var that = this
    app.req({
      url: 'wechat/actcomments/detailList',
      method: 'post',
      data: {
        id:that.data.id
      },
      success: function (res) {
        if (res.code == 200) {
         console.log('列表',res)
         that.setData({
           list:res.data,
           activityId: res.data[0].activityId
         })
         var num = res.data.length
          wx.setNavigationBarTitle({
            title: num +'条回复'
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 2000
          })
        }
      }
    })
  },
  //回复
  reply: function (e) {
    var nameid = e.currentTarget.dataset.name
    var hfindex = e.currentTarget.dataset.index
    console.log('回复：' + (hfindex), e)
    this.setData({
      parentId: e.currentTarget.id,
      isFather: false,
      focus: true,
      hfindex: hfindex,
      replytex: '回复' + nameid + ':'
    })
  },
  bindblur: function () {
    this.setData({
      focus: false
    })
  },
  //留言
  formName: function (e) {
    this.setData({
      inputname: e.detail.value.replace(/\s+/g, '')
    })
  },
  //确认  
  confirmreply: function () {
    let that = this
    console.log(that.inputname)
    if (that.data.inputname == '') {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (that.data.isFather) {
        console.log('true')
        var data = {
          "activityId": that.data.activityId,
          "content": that.data.inputname,
          "parentId": that.data.list[0].id
        }
      } else {
        console.log('false')
        var data = {
          "activityId": that.data.activityId,
          "content": that.data.inputname,
          "parentId": that.data.parentId
        }
      }
      app.req({
        url: "wechat/actcomments/save",
        method: "post",
        data: data,
        success: function (res) {
          wx.showToast({
            title: '留言成功',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            inputname: '',
            replytex: '写评论',
            isFather:true,
          })
          that.getdetails()

        }
      });
    }
    this.setData({
      focus: false
    })
  },
  //删除
  deletereply: function (event) {
    var that = this;
    var iszy = event.currentTarget.dataset.zy
    console.log(event.currentTarget.id)
    let id = event.currentTarget.id
    let data = {
      "id": event.currentTarget.id,
      "activityId": that.data.activityId,
    }
    wx.showModal({
      title: '提示',
      content: '您确定要删除当前评论吗?',
      confirmColor: '#2168EB',
      success(res) {
        if (res.confirm) {
          app.req({
            url: "wechat/actcomments/deleteComments",
            method: "post",
            data: data,
            success: function (res) {
              wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 2000
              })
              if (iszy == 1) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                that.getdetails()
              }
            }
          });
        }
      }
    })
  },
  //评论图片
  fdimg(e) {
    console.log('图片', e)
    var arr = this.data.list[0].picturesUrl
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: arr[index],     //当前图片地址
      urls: arr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    if (options.istrue==1){
      this.setData({
        focus:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo,
      })
    })
    that.getdetails()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})